﻿using KFC.MonAnF;
using KFC.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KFC
{
    public partial class LoadMonAnFromXML : Form
    {
        private String fileXML;
        private List<Element> elements = new List<Element>();
        private List<string> xml = new List<string>();
        private int numberField;
        private List<MapField2Element> maps;
        public LoadMonAnFromXML()
        {
            InitializeComponent();
        }

        private void button_Convert_Click(object sender, EventArgs e)
        {
            MappingXML2Object m2o = new MappingXML2Object(xml);
            m2o.ShowDialog();
            maps = m2o.Maps;

            List<MonAn> list = SaveData();
            if (list != null)
            {
                QuanLyMonAn qlma = QuanLyMonAn.Instance();
                foreach (var st in list)
                {
                    qlma.ThemMon(st);
                }
            }
        }

        public List<MonAn> SaveData()
        {
            List<MonAn> MonAns = new List<MonAn>();
            int rows = elements.Count / numberField;
            // duyen tung student
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < numberField; j++)
                {
                    // ten cua thuoc tinh nam trong mang 
                    // list_element cua map_element 
                    for (int k = 0; k < maps.Count; k++)
                    {
                        string temp = elements[i * numberField + j].Name;
                        if (maps[k].Element.Exists(el => el == temp))
                        {
                            maps[k].Insert(elements[i * numberField + j].Content);
                        }
                    }
                }
                try
                {
                    MonAn ma = MonAnFactory.Produce(maps[5].InsertElement);
                    ma.MaMonAn = maps[0].InsertElement;
                    ma.TenMonAn = maps[1].InsertElement;
                    ma.DonGia = int.Parse(maps[2].InsertElement);
                    ma.ChiTietMonAn = maps[3].InsertElement;
                    ma.SoLuong = int.Parse(maps[4].InsertElement);
                    ma.LoaiDoiTuong = maps[5].InsertElement;

                    
                    MonAns.Add(ma);
                    foreach (var v in maps)
                        v.Clear();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Cannot mapping...");
                    return null;
                }
            }
            return MonAns;
        }

        private void button_open_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "C:\\";
            openFileDialog1.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;


            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        textBox_FileName.Text = openFileDialog1.FileName;
                        fileXML = openFileDialog1.FileName;
                        XML_parse parse = new XML_parse(fileXML);
                        elements = parse.GetAllElement("CSDLMONAN//MonAn");
                        numberField = parse.NumberElement;
                        for (int i = 0; i < parse.NumberElement; i++)
                        {
                            listBox_xml.Items.Add(elements[i].Name);
                            xml.Add(elements[i].Name);
                        }
                        button_Convert.Enabled = true;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }

        
    }
}
